<?php
namespace Home\Model;
use Think\Model;

/**
* Created by hanquan.
* User user,master
* Date 2017/3/29 
* Time 21:19
* RecycleOrderModel 回收订单类
*/
class RecycleOrderModel extends Model{

	private $RecycleOrder = null;
	function __construct(){
		$this->RecycleOrder = M('RecycleOrder');
	}

	/**
	 * 获取师傅的回收订单
	 * @param $mid 师傅id
	 */
	public function get_master_order($mid){
		
		$condition['mid'] = $mid;
		return $this->RecycleOrder->where($condition)->order('update_time desc')->join('mp_version on mp_recycle_order.vid=mp_version.vid','LEFT')->select();
	}

	/**
	 * 查询回收订单条数
	 * @param  int  $mid    师傅id
	 * @param  int $status  订单状态，-1-未完成，0-待处理，1-待接单，2-已接单，3-待确认， 4-客户取消订单，5-待回收，6-已回收，7-已完成
	 * @return int          回收订单条数
	 */
	public function master_order_count($mid,$status=FALSE){
		$condition['mid'] = $mid;
		if($status){
			$condition['status'] = $status;
		}
		return $this->RecycleOrder->where($condition)->count();
	}

	/**
	 * 获取用户的回收订单
	 * @param $uid 用户id
	 */
	public function get_user_order($uid){

		$condition['uid'] = $uid;
		$condition['status'] = array('egt',0);
		return $this->RecycleOrder
		->join('mp_version ON mp_recycle_order.vid = mp_version.vid','LEFT')
		->field('rid,order_num,status,update_time,v_name,price,repair_way,valuation')
		->where($condition)->order('rid desc')->select();
	}

	/**
	 * 获取回收订单
	 * @param $rid 回收订单id
	 */
	public function get_order($rid){

		$condition['rid'] = $rid;
		return $this->RecycleOrder->join('mp_version ON mp_recycle_order.vid=mp_version.vid','LEFT')
		->join('mp_region ON mp_recycle_order.region=mp_region.re_id','LEFT')
		->join('mp_city ON mp_region.city_id=mp_city.city_id','LEFT')
		->join('mp_member ON mp_recycle_order.mid=mp_member.member_id','LEFT')
		->field('rid,m_tel,mp_recycle_order.mid,mp_recycle_order.vid,phone_number,repair_way,mp_info,door_time,stores,mp_city.city,mp_region.region,address,note,order_num,valuation,price,v_name,m_name,m_account,status')
		->where($condition)->find();
	}

	/**
	 * 获取某工程师今天订单数量
	 * @param  int $member_id 成员id
	 * @return int 订单数量
	 */
	public function get_today_order($member_id){
		$condition['update_time'] = array('like',date('Y-m-d').'%');
		$condition['mid'] = $member_id;
		return $this->RecycleOrder->where($condition)->select();
	}

	/**
     * 插入或修改回收订单
     * @param $data 提交回收订单的数据
     * @param $rid 回收订单id
     */
	public function set_order($data,$rid=FALSE){
		if(!$rid){
			return $this->RecycleOrder->add($data);
		}else{
			$condition['rid'] = $rid;
			return $this->RecycleOrder->where($condition)->save($data);
		}
	}

	/**
     * 删除回收订单
     * @param rid 回收订单id
     */
	public function del_order($rid){

		$condition['rid'] = $rid;
		return $this->RecycleOrder->where($condition)->delete();
	}

}